package com.ccl.iot.privates;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;

import com.ccl.iot.device.IOTAirCondition;
import com.ccl.iot.IOTNetAPIEnum;

public class AirConditionIconBuilder {
	//==============modify start===============
	public synchronized static Bitmap Build(int aWidth, int aHeight, int aHighLightColor, int aBackColor){
		if (aWidth > 0 && aHeight > 0){
			Bitmap tIcon;
			String tKey = "AirConditionIcon:" + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor +"Turn";
			
			if ((tIcon = BitmapCollector.Get(tKey)) == null){
				tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);
				
				Canvas tCanvas = new Canvas(tIcon);
				Paint tPaint = new Paint();
				float tRefLen = 0, tCenterX = 0, tCenterY = 0, tXOffset = 0, tYOffset = 0;
				
				if (aWidth > aHeight){
					tCenterY = tRefLen = aHeight * 0.5f;
					tXOffset = (aWidth - aHeight) * 0.5f;
					tCenterX = tCenterY + tXOffset;
				}else{
					tCenterX = tRefLen = aWidth * 0.5f;
					tYOffset = ((aHeight - aWidth) * 0.5f);
					tCenterY = tCenterX + tYOffset;
				}
				
				if (aBackColor != Color.TRANSPARENT){
					tCanvas.drawColor(aBackColor);
				}
				
				tPaint.setAntiAlias(true);
				tPaint.setFilterBitmap(true);
				
				tPaint.setColor(aHighLightColor);
				tPaint.setStyle(Paint.Style.FILL);
//				tCanvas.drawCircle(tCenterX, tCenterY, tRefLen, tPaint);
				
//				RadialGradient tGradient = new RadialGradient(tCenterX, tCenterY, tRefLen * 0.95f, new int[]{Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.LTGRAY, Color.GRAY}, null, Shader.TileMode.CLAMP);
//				tPaint.setShader(tGradient);
//				
//				tCanvas.drawCircle(tCenterX, tCenterY, tRefLen * 0.95f, tPaint);

				Path tPath = new Path();
				tPath.moveTo(tRefLen * 0.845f + tXOffset, tRefLen * 0.453f + tYOffset);
				tPath.arcTo(new RectF(tRefLen * 0.405f + tXOffset, tRefLen * 0.433f + tYOffset, tRefLen * 1.594f + tXOffset, tRefLen * 1.622f + tYOffset), -105, -330);
				tPath.lineTo(tRefLen * 1.155f + tXOffset, tRefLen * 0.615f + tYOffset);
				tPath.arcTo(new RectF(tRefLen * 0.559f + tXOffset, tRefLen * 0.586f + tYOffset, tRefLen * 1.441f + tXOffset, tRefLen * 1.468f + tYOffset), -69, 319);
				tPath.close();
				
				tPaint.setShader(null);
				tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
				tPaint.setColor(aHighLightColor);
				tCanvas.drawPath(tPath, tPaint);
				
				tCanvas.drawRect(new RectF(tRefLen * 0.924f + tXOffset, tRefLen * 0.332f + tYOffset, tRefLen * 1.076f + tXOffset, tRefLen * 0.705f + tYOffset), tPaint);
				
				tCanvas.drawCircle(tCenterX, tRefLen * 0.332f + tYOffset, tRefLen * 0.076f, tPaint);
				tCanvas.drawCircle(tCenterX, tRefLen * 0.705f + tYOffset, tRefLen * 0.076f, tPaint);
				
				
				tCanvas.save();
				tCanvas.restore();
				
				BitmapCollector.Add(tKey, tIcon);
				
				return tIcon;
			}else{
				return tIcon;
			}
		}
		
		return null;
	}
	//==============modify end===============
	
	public synchronized static Bitmap Build(int aWidth, int aHeight, int aHighLightColor, int aBackColor, BitmapMask aMask){
	if (aWidth > 0 && aHeight > 0){
		Bitmap tIcon;
		String tKey = "AirConditionIcon:" + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode());
		
		if ((tIcon = BitmapCollector.Get(tKey)) == null){
			tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);
			
			Canvas tCanvas = new Canvas(tIcon);
			Paint tPaint = new Paint();
			float tRefLen = 0, tCenterX = 0, tCenterY = 0, tXOffset = 0, tYOffset = 0;
			
			if (aWidth > aHeight){
				tCenterY = tRefLen = aHeight * 0.5f;
				tXOffset = (aWidth - aHeight) * 0.5f;
				tCenterX = tCenterY + tXOffset;
			}else{
				tCenterX = tRefLen = aWidth * 0.5f;
				tYOffset = ((aHeight - aWidth) * 0.5f);
				tCenterY = tCenterX + tYOffset;
			}
			
			if (aBackColor != Color.TRANSPARENT){
				tCanvas.drawColor(aBackColor);
			}
			
			tPaint.setAntiAlias(true);
			tPaint.setFilterBitmap(true);
			
			tPaint.setColor(aHighLightColor);
			tPaint.setStyle(Paint.Style.FILL);
			tCanvas.drawCircle(tCenterX, tCenterY, tRefLen, tPaint);
			
			RadialGradient tGradient = new RadialGradient(tCenterX, tCenterY, tRefLen * 0.95f, new int[]{Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.LTGRAY, Color.GRAY}, null, Shader.TileMode.CLAMP);
			tPaint.setShader(tGradient);
			
			tCanvas.drawCircle(tCenterX, tCenterY, tRefLen * 0.95f, tPaint);

			Path tPath = new Path();
			tPath.moveTo(tRefLen * 0.845f + tXOffset, tRefLen * 0.453f + tYOffset);
			tPath.arcTo(new RectF(tRefLen * 0.405f + tXOffset, tRefLen * 0.433f + tYOffset, tRefLen * 1.594f + tXOffset, tRefLen * 1.622f + tYOffset), -105, -330);
			tPath.lineTo(tRefLen * 1.155f + tXOffset, tRefLen * 0.615f + tYOffset);
			tPath.arcTo(new RectF(tRefLen * 0.559f + tXOffset, tRefLen * 0.586f + tYOffset, tRefLen * 1.441f + tXOffset, tRefLen * 1.468f + tYOffset), -69, 319);
			tPath.close();
			
			tPaint.setShader(null);
			tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
			tPaint.setColor(aHighLightColor);
			tCanvas.drawPath(tPath, tPaint);
			
			tCanvas.drawRect(new RectF(tRefLen * 0.924f + tXOffset, tRefLen * 0.332f + tYOffset, tRefLen * 1.076f + tXOffset, tRefLen * 0.705f + tYOffset), tPaint);
			
			tCanvas.drawCircle(tCenterX, tRefLen * 0.332f + tYOffset, tRefLen * 0.076f, tPaint);
			tCanvas.drawCircle(tCenterX, tRefLen * 0.705f + tYOffset, tRefLen * 0.076f, tPaint);
			
			int size = aWidth > aHeight ? aHeight : aWidth;
			
			if (aMask != null){
				aMask.paintMask(tCanvas);
			}else{
				Paint paint = new Paint();
				paint.setAntiAlias(true);
				paint.setFilterBitmap(true);
				paint.setColor(Color.GRAY);
				float x = (float)(size*0.4)/2;
				tCanvas.drawCircle(x, x, x, paint);
			}
			
			tCanvas.save();
			tCanvas.restore();
			
			BitmapCollector.Add(tKey, tIcon);
			
			return tIcon;
		}else{
			return tIcon;
		}
	}
	
	return null;
}
	
	/*// =============================modify start====================================
			public synchronized static Bitmap Build(int aWidth, int aHeight, int aHighLightColor, int aBackColor, boolean isOn, boolean isPressed, BitmapMask aMask, int iconId) {
				if (aWidth > 0 && aHeight > 0) {
					Bitmap tIcon;
					String tKey = "AirConditionIcon:" + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode()) + "." + iconId;

					// System.out.println("SwitchIconBuilder---> DeviceIconProvider.ICON_ID: "+iconId);

					if ((tIcon = BitmapCollector.Get(tKey)) == null) {
						tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_4444);

						Canvas tCanvas = new Canvas(tIcon);

						Bitmap centerIcon = null;

						// TODO 设置设备页面空调的图标和批处理图标
						if(isPressed){
									centerIcon = Bitmap.createBitmap(aWidth, aHeight, Config.ARGB_4444);
										Canvas can = new Canvas(centerIcon);
										can.drawColor(DeviceIconProvider.ICON_LIST_SWITCH_PRESS[iconId]);
							
										can.drawColor(DeviceIconProvider.ICON_LIST_SWITCH_PRESS[DeviceIconProvider.ALL_ICON_ID]);
						}else{
							if(isOn){
								centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_ON[iconId]);
							}else{
								centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_OFF[iconId]);
							}
						}
						Paint paint = new Paint();

						int size = aWidth > aHeight ? aHeight : aWidth;

						centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

						float left = (aWidth - centerIcon.getWidth()) / 2;
						float top = (aHeight - centerIcon.getHeight()) / 2;

						if (aBackColor != Color.TRANSPARENT) {
							tCanvas.drawColor(aBackColor);
						}

						tCanvas.drawBitmap(centerIcon, left, top, paint);

						if (aMask != null) {
							aMask.paintMask(tCanvas);
						}

						tCanvas.save();
						tCanvas.restore();

						BitmapCollector.Add(tKey, tIcon);

						return tIcon;
					} else {
						return tIcon;
					}
				}

				return null;
			}

			// =============================modify end====================================
*/
			public static Bitmap Build(int aWidth, int aHeight, boolean aOnline, boolean aIsOn, boolean aPressing, int aAuth, int iconId) {
				Bitmap tIcon = null;
				int tColor = Color.DKGRAY;

				if (aOnline) {
					BitmapMask tMask = null;

					if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_UNKNOWN || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE
							|| aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_WAITTING || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_REJECT || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_QUIT) {
						aIsOn = false;
						aPressing = false;
						tMask = new ForbidMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
					} else if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_READ) {
						aPressing = false;
						tMask = new WarningMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
					}

					if (aIsOn) {
						if (aPressing) {
							tColor = Color.rgb(100, 200, 100);
						} else {
							tColor = Color.GREEN;
						}
					} else {
						if (aPressing) {
							tColor = Color.rgb(200, 100, 100);
						} else {
							tColor = Color.RED;
						}
					}

					
					tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, tMask);				
					}else{
					tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, null);
					for (int i = 0; i < tIcon.getWidth(); i+=3) {
						for (int j = 0; j < tIcon.getHeight(); j+=3) {
							int pixel = tIcon.getPixel(i, j);
							
							tIcon.setPixel(i, j, pixel & 0x32323232);
						}
					}
					}
					

					/*// ==============================modify start===========================================

					tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, aIsOn, aPressing, tMask, iconId);
				} else {
					tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, aIsOn, aPressing, null, iconId);
					for (int i = 0; i < tIcon.getWidth(); i+=3) {
						for (int j = 0; j < tIcon.getHeight(); j+=3) {
							int pixel = tIcon.getPixel(i, j);
							
							tIcon.setPixel(i, j, pixel & 0x32323232);
						}
					}
				}
				// ==============================modify end===========================================
*/
				return tIcon;
			}

			public static Bitmap Build(IOTAirCondition aDevice, int aWidth, int aHeight, boolean aPressing) {
				if (aDevice != null) {
					return Build(aWidth, aHeight, aDevice.isOnline(), aDevice.isOn(), aPressing, aDevice.getAuth(), aDevice.getSplitIconId());
				}

				return null;
			}

}
